<?php
if (!empty($_GET)){
	$err = array();
	    	
	if (!is_numeric($_GET['vyska']) || $_GET['vyska']<=0){
		$err['amount'] = 'Nespravny vstup - vyska';
	}
	    	
	if (!is_numeric($_GET['vaha']) || $_GET['vaha']<=0){
	    	$err['percent'] = 'Nespravny vstup - vaha';
	}	
}

$showForm = true;


if (isset($err)){
	if (!empty($err)){
		$showForm = true;
	
		foreach ($err as $current){
			echo "".$current."</div><br/>";
		}
	}
	else{
	    	$bmi = ($_GET['vaha']/ ($_GET['vyska']/100*$_GET['vyska']/100));
        if($bmi <= '16,5')
        {
          $status = 'těžká podvýživa';
        } 
        else if($bmi > '16,5' && $bmi <= '18,5')
        {
          $status = 'podváha';
        }
        else if($bmi > '18,5' && $bmi <= '25')
        {
          $status =  'ideální váha';
        }
        else if($bmi > '25' && $bmi <= '30')
        {
          $status = 'nadváha';
        }
        else if($bmi > '30' && $bmi <= '35')
        {
          $status = 'mírná obezita';
        }
        else if($bmi > '35' && $bmi <= '40')
        {
          $status = 'střední obezita';
        }
        else if($bmi > '40')
        {
          $status = 'morbidní obezita';
        }
	}
	$showForm = false;
}

?>

<?php
if ($showForm){
?>
<!DOCTYPE html>
<html>
<head>
  <title>Úkol 4</title>
  <meta charset="utf-8">
</head>
<body>
<form action="ukol4.php" method="get">
	<h2>Výpočet BMI</h2>
  <br/>

	<label for="jmeno"> Jméno </label>
	<input type="text" id="jmeno" name="jmeno" value="<?php echo $_GET['jmeno'];?>">
		
	<br/><br/>
	<label for="vyska"> Výška (cm) </label>
	<input type="number" id="vyska" name="vyska" value="<?php echo $_GET['vyska'];?>">

	<br/><br/>
	<label for="vaha"> Váha (kg)) </label>
	<input type="number" id="vaha" name="vaha" value="<?php echo $_GET['vaha'];?>">
  <br/><br/>
	<input type="submit" value="Vypočítat">
</form>
</body>
</html>

<?php
}else{ // else if ($showForm)
	echo "
	<!DOCTYPE html>
	    <html>
	      <head>
		<title>Úkol 4</title>
		<meta charset=\"utf-8\">
		<style>
		   .cervena{color:red;}
		</style>
	      </head>
	      <body>
		<h2>Výpočet BMI</h2>
		<br/>
		<table>
		  <tr>
		    <td colspan=\"2\">".$_GET['jmeno']."</td>
		  </tr>
		  <tr>
		    <td>Vyska</td>
		    <td>".$_GET['vyska']."</td>
		  </tr>
		  <tr>
		    <td>Vaha</td>
		    <td>".$_GET['vaha']."</td>
		  </tr>
		  <tr>
		    <td>BMI</td>
		    <td>".$bmi."</td>
		  </tr>
		  <tr>
		    <td>Kategorie</td>";
		    if($bmi > '25' || $bmi < '20')
		    {
		      echo "<td class=\"cervena\">".$status."</td>";
		    }
		    else
		    {
		      echo "<td>".$status."</td>";
		    }
		    echo "           
		  </tr>
		</table>  
  ";
} // end if ($showForm)
?>